package com.example.hadoop.mapreduce.inverindex;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: Brian
 * @Date: 2020/04/28/9:36
 * @Description: hello#a.txt-->3
 * hello#b.txt-->2
 * hello#c.txt-->2
 * jerry#a.txt-->1
 * jerry#b.txt-->3
 * jerry#c.txt-->1
 * tom#a.txt-->2
 */
public class InverIndexStepTwo {

    static class InverIndexStepTwoMapper extends Mapper<LongWritable, Text, Text, Text> {
        Text k = new Text();
        Text v = new Text();

        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String line = value.toString();
            String[] words = line.split("#");
            k.set(words[0]);
            v.set(words[1]);
            context.write(k, v);
        }
    }

    /**
     * 输入：
     * <hello,a.txt-->1><hello,b.txt-->1> ...
     */
    static class InverIndexStepTwoReducer extends Reducer<Text, Text, Text, Text> {
        Text v = new Text();

        @Override
        protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
            StringBuffer line = new StringBuffer();
            for (Text text : values) {
                line.append(text).append("\t");
            }
            if (line.length() > 0) {
                line.deleteCharAt(line.length() -1);
            }
            v.set(line.toString());
            context.write(key, v);
        }
    }

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        if (args == null || args.length < 2) {
            args[0] = "/wordcount/flowinput";
            args[1] = "/wordcount/flowoutput";
        }

        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);

        //设置jar包路径
        job.setJarByClass(InverIndexStepTwo.class);

        //设置Map Reduce类
        job.setMapperClass(InverIndexStepTwoMapper.class);
        job.setReducerClass(InverIndexStepTwoReducer.class);

        //设置Map输出
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);
        //设置最终输出
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);

        //设置输入输出路径
        FileInputFormat.setInputPaths(job, new Path(args[0]));
        Path outputPath = new Path(args[1]);
        FileSystem fileSystem = FileSystem.get(conf);
        if (fileSystem.exists(outputPath)) {
            fileSystem.delete(outputPath, true);
        }
        FileOutputFormat.setOutputPath(job, outputPath);

        boolean successful = job.waitForCompletion(true);
        System.out.println("Successfully? --> " + successful);
        System.exit(successful ? 0 : 1);
    }
}
